var app = getApp();
var util = require('../../utils/util.js');
Page({

  data: {
    fansList: []
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    this.getFansList("",false);
  },

  onReachBottom: function() {
    var fansList = this.data.fansList;
    var fans = fansList[fansList.length - 1];
    var time = fans.messageDate;
    this.getFansList(time, true);
  },
  //关注
  attention: function(e) {
    var index = e.currentTarget.dataset.index;
    var fans = this.data.fansList[index];
    if (fans.attentionDate == null) {
      fans.attentionDate = 'A';
      fans.fansQuantity = fans.fansQuantity + 1;
      fans.showFansQuantity = util.formatQuantity(fans.fansQuantity);
      this.attentionReq(1, fans);
    } else {
      fans.attentionDate = null;
      fans.fansQuantity = fans.fansQuantity - 1;
      fans.showFansQuantity = util.formatQuantity(fans.fansQuantity);
      this.attentionReq(2, fans);
    }
    this.setData({
      fansList: this.data.fansList
    })
  },

  //关注或取消关注
  attentionReq: function(type, fans) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'addAttention',
      method: 'POST',
      data: {
        userId: fans.fansId,
        fansId: app.globalData.user.id,
        type: type
      }
    })
  },
  //获取粉丝信息
  getFansList: function (time, reach) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getFansList',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        time: time
      },
      success: function(r) {
        if (r.data.code == 200) {
          for (var i of r.data.data) {
            i.showConcernQuantity = util.formatQuantity(i.concernQuantity);
            i.showPetQuantity = util.formatQuantity(i.petQuantity);
            i.showFansQuantity = util.formatQuantity(i.fansQuantity);
          }
          if (reach&&r.data.data.length == 0) {
            wx.showToast({
              title: '没有更多记录了',
              icon: 'none',
              duration: 3000
            })
          }
          that.setData({
            fansList: that.data.fansList.concat(r.data.data)
          })
        } else {
          wx.showToast({
            title: '获取粉丝列表出了点问题,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,获取粉丝列表失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  }

})